import { drawLine } from "@/widgets/line-plot/drawLine"

/**
 * @type {DE.Viewer}
 */
let map // 地图对象
const Cesium = DE.__namespace.Cesium

// 初始化当前业务
export function onMounted(mapInstance) {
  map = mapInstance // 记录map
  map.tooltip.enable = true
}

// 释放当前业务
export function onUnmounted() {
  map = null
}

export function drawLineBetweenOverlays() {
  // 通信线绘制
  drawLine({
    type: "straightLine", // 直线相关定义，通信线，指挥线、打击线等都属于直线范畴，只是 type、style、连接条件不同
    layer: window.$actionPlotLayer,
    style: {
      material: new DE.PolylineImageTrailMaterialProperty({
        color: DE.Color.GREEN,
        speed: 5,
        image: "img/equip/communication_line.png",
        repeat: {
          x: 50,
          y: 1
        }
      }),
      clampToGround: true,
      width: 10
    },
    attr: {
      type: "communication_line"
    },
    rules: {
      clickRuleCheck: (overlay, line) => {
        if (!overlay || overlay.attr?.category !== 1010) {
          window?.$message("请在装备上左击！")
          return false
        }
        return true
      },
      secondClickRuleCheck: (overlay, line, pickOverlayList) => {
        const [firstOverlay, secondOverlay] = pickOverlayList
        const lines = window.$actionPlotLayer.getOverlays()
        if (secondOverlay.overlayId === firstOverlay.overlayId) {
          window?.$message("请在另一个装备上左击！")
          return false
        }
        if (
          lines.filter(
            (line) =>
              line.attr.type === "communication_line" &&
              line.attr.equipIds.includes(firstOverlay.overlayId) &&
              line.attr.equipIds.includes(secondOverlay.overlayId)
          ).length
        ) {
          window?.$message("已存在通信，请重新绘制")
          return false
        }
        return true
      }
    },
    callback: (line) => {
      console.log(line)
    }
  })
}

export function drawCommandLine() {
  // 指挥线
  drawLine({
    type: "straightLine", // 直线相关定义，通信线，指挥线、打击线等都属于直线范畴，只是 type、style、连接条件不同
    layer: window.$actionPlotLayer,
    style: {
      color: "yellow",
      material: new DE.PolylineArrowMaterialProperty(DE.Color.PURPLE),
      width: 5
    },
    attr: {
      type: "command_line"
    },
    rules: {
      clickRuleCheck: (overlay, line) => {
        if (!overlay || overlay.attr?.category !== 1010) {
          window?.$message("请在装备上左击！")
          return false
        }
        return true
      },
      secondClickRuleCheck: (overlay, line, pickOverlayList) => {
        const [firstOverlay, secondOverlay] = pickOverlayList
        const lines = window.$actionPlotLayer.getOverlays()
        if (secondOverlay.overlayId === firstOverlay.overlayId) {
          window?.$message("请在另一个装备上左击！")
          return false
        }
        if (
          lines.filter(
            (line) =>
              line.attr.type === "command_line" &&
              line.attr.equipIds.includes(firstOverlay.overlayId) &&
              line.attr.equipIds.includes(secondOverlay.overlayId)
          ).length
        ) {
          window?.$message("已存在指挥线，请重新绘制")
          return false
        }
        return true
      }
    },
    callback: (line) => {
      console.log(line)
    }
  })
}

export function drawLineBetweenOverlaysWithConstraint() {
  drawLine({
    type: "straightLine", // 直线相关定义，通信线，指挥线、打击线等都属于直线范畴，只是 type、style、连接条件不同
    layer: window.$actionPlotLayer,
    style: {
      color: "yellow",
      width: 4
    },
    attr: {
      type: "restrict_line"
    },
    rules: {
      clickRuleCheck: (overlay, line) => {
        if (!overlay || overlay.attr?.category !== 1010) {
          window?.$message("请在装备上左击！")
          return false
        }
        return true
      },
      secondClickRuleCheck: (overlay, line, pickOverlayList) => {
        const [firstOverlay, secondOverlay] = pickOverlayList
        const lines = window.$actionPlotLayer.getOverlays()
        if (secondOverlay.overlayId === firstOverlay.overlayId) {
          window?.$message("请在另一个装备上左击！")
          return false
        }
        if (firstOverlay.name.split("-")[1] >= secondOverlay.name.split("-")[1]) {
          window?.$message("船舶名称只能小联大！")
          return false
        }
        if (
          lines.filter(
            (line) =>
              line.attr.type === "restrict_line" &&
              line.attr.equipIds.includes(firstOverlay.overlayId) &&
              line.attr.equipIds.includes(secondOverlay.overlayId)
          ).length
        ) {
          window?.$message("已存在约束线，请重新绘制")
          return false
        }
        return true
      }
    },
    callback: (line) => {
      console.log(line)
    }
  })
}
